package tools.aligning;

import tools.sequences.sequenceUtils;

public class LocalMutation implements Comparable<LocalMutation>{

	private int position;
	private String ref,mutation;
	
	public LocalMutation(LocalMutation lm){
		this(lm.getPosition(),lm.getRef(),lm.getMutation());
	}
	
	public LocalMutation(int position, String ref, String mutation){
		setPosition(position);
		setRef(ref);
		setMutation(mutation);
	}
	
	public LocalMutation(int position, char ref, char mutation){
		this(position,Character.toString(ref),Character.toString(mutation));
	}
	
	private LocalMutation(String[] l){
		this(Integer.parseInt(l[0]),l[1],l[2]);
	}
	
	public LocalMutation(String s){
		this(s.split("[:>]"));
	}
	
	public boolean encapsulated(int start, int end){
		return getPosition()>=start && getPosition()<end;
	}
	
	public String EMS(){
		if(getRef().charAt(0)=='A' && getMutation().charAt(0)=='G'){
			return "R";
		}
		if(getRef().charAt(0)=='C' && getMutation().charAt(0)=='T'){
			return "M";
		}
		if(getRef().charAt(0)=='G' && getMutation().charAt(0)=='A'){
			return "M";
		}
		if(getRef().charAt(0)=='T' && getMutation().charAt(0)=='C'){
			return "R";
		}
		return "U";
	}
	
	public void updatePos(int newPos){
		setPosition(newPos);
	}
	
	public void shift(int refPoint){
		this.updatePos(getPosition()+refPoint);
	}
	
	public void updatePosRevComp(int newPos){
		setPosition(newPos);
		setRef(sequenceUtils.reverseComplement(getRef()));
		setMutation(sequenceUtils.reverseComplement(getMutation()));
	}
	
	public void shiftRevComp(int refPoint){
		this.updatePosRevComp(refPoint-getPosition());
	}

	public int getPosition() {
		return position;
	}

	public String getRef() {
		return ref;
	}

	public String getMutation() {
		return mutation;
	}

	private void setPosition(int position) {
		this.position = position;
	}

	private void setRef(String ref) {
		this.ref = ref;
	}

	private void setMutation(String mutation) {
		this.mutation = mutation;
	}
	
	public String toString(){
		return getPosition()+":"+getRef()+">"+getMutation();
	}

	@Override
	public int compareTo(LocalMutation o) {
		// TODO Auto-generated method stub
		return this.getPosition()-o.getPosition();
	}
}
